package com.itheima.a04udpdemo3;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Scanner;

public class SendMessageDemo {
    public static void main(String[] args) throws IOException {
        //使用Udp发送数据 组播

        //创建MulticastSocket对象,随机一个端口进行发送
        MulticastSocket ms = new MulticastSocket();

        //打包数据
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要发送的数据");
            //发送什么数据
            String str = sc.nextLine();
            if("886".equals(str)){
                break;
            }
            byte[] bytes = str.getBytes();
            //发送到哪里
            InetAddress ip = InetAddress.getByName("224.0.0.255");
            //发送的端口号
            int port = 10086;
            DatagramPacket dp = new DatagramPacket(bytes, bytes.length, ip, port);

            //发送
            ms.send(dp);
        }

        //释放资源
        ms.close();
    }
}
